pot:: Int -> Int -> Int
pot 0 m = 1
pot (n+1) m = m*(pot(n) m)

triomino:: Int -> (Int,Int) -> [(Int,Int)]
triomino 0 _   = []
triomino (n+1) (x,y) 
	-- primer cuadrante
    | (x < pot n 2) && (y >= pot n 2) = triomino n (x,y-(pot n 2)) ++ a ++ b ++ c ++ z
    -- segundo cuadrante	    
    | (x < pot n 2) && (y < pot n 2) = triomino n (x,y) ++ d ++ e ++ f ++ o 
    -- tercer cuadrante             
    | (x >= pot n 2) && (y < pot n 2) = triomino n (x-(pot n 2),y) ++ g ++ h ++ i ++ w
	-- cuarto cuadrante
  	|(x >= pot n 2) && (y >= pot n 2) = triomino n (x-(pot n 2),y-(pot n 2)) ++ j ++ k ++ l ++ s
		where
			a' = triomino n (0,0)
  			b' = triomino n ((pot n 2)-1,(pot n 2)-1) 
  			c' = triomino n (0,(pot n 2)-1) 
  			a  = map (offset (pot n 2) (pot n 2)) a'
  			b  = map (offset 0 0) b'
  			c  = map (offset (pot n 2) 0) c'
			z  = [(pot n 2,(pot n 2)-1)] ++ [(pot n 2,pot n 2)] ++ [((pot n 2)-1,(pot n 2)-1)]
            
			o  = [(pot n 2,(pot n 2)-1)] ++ [(pot n 2,pot n 2)] ++ [((pot n 2)-1,pot n 2)]
			d' = triomino n ((pot n 2)-1,0)
			e' = triomino n (0,(pot n 2)-1)
  			f' = triomino n (0,0) 
  			d  = map (offset 0 (pot n 2)) d'
  			e  = map (offset (pot n 2) 0) e'
  			f  = map (offset (pot n 2) (pot n 2)) f' 
			g' = triomino n ((pot n 2)-1,(pot n 2)-1) 
			h' = triomino n (0,0) 
  			i' = triomino n ((pot n 2)-1,0)
  			g  = map (offset 0 0) g'
  			h  = map (offset (pot n 2) (pot n 2)) h'
  			i  = map (offset 0 (pot n 2)) i'
			s = [(pot n 2,(pot n 2)-1)] ++ [((pot n 2)-1,(pot n 2)-1)] ++ [((pot n 2)-1,pot n 2)]
			w = [((pot n 2)-1,(pot n 2)-1)] ++ [(pot n 2,pot n 2)] ++ [((pot n 2)-1,pot n 2)]
			j' = triomino n (0,(pot n 2)-1)
			k' = triomino n ((pot n 2)-1,0) 
  			l' = triomino n ((pot n 2)-1,(pot n 2)-1)
  			j  = map (offset (pot n 2) 0) j'
  			k  = map (offset 0 (pot n 2)) k'
  			l  = map (offset 0 0) l'

offset:: Int -> Int -> (Int,Int) -> (Int,Int) 
offset x y (x1,y1) = (x1+x,y1+y)

trio:: Int -> (Int,Int) -> [(Int,Int)]
trio n (x,y) = trioRec 3 (n-1) (x,y) (triomino n (x,y)) []

trioRec:: Int -> Int -> (Int,Int) -> [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)]
trioRec 0 _ _ ls zs = ls ++ zs
trioRec (n+1) m (x,y) (l:ls) zs
	-- primer cuadrante
    | (x < pot m 2) && (y >= pot m 2) = trioRec n m (x,y) ls ((offset 0 (pot m 2) l):zs) 
    -- segundo cuadrante	    
    | (x < pot m 2) && (y < pot m 2) = zs ++ (l:ls)
    -- tercer cuadrante             
    | (x >= pot m 2) && (y < pot m 2) = trioRec n m (x,y) ls ((offset (pot m 2) 0 l):zs)
	-- cuarto cuadrante
  	|(x >= pot m 2) && (y >= pot m 2) = trioRec n m (x,y) ls ((offset (pot m 2) (pot m 2) l):zs)
